<%@page contentType="application/json;charset=UTF-8" %><%@page import="be.destin.skos.core.SkosManager" %><%@page import="be.destin.skos.html.SkosHtmlManager" %><%@page import="be.destin.skos.core.SchemeInterface" %><%@page import="be.destin.memolog.UsageMode" %><%@page import="be.destin.memolog.UsageLogger" %><%@page import="be.destin.memo.Log" %><%@page import="be.destin.memo.TagDef" %><%@page import="be.destin.memo.Tagged" %><%@page import="be.destin.records.UsageManager" %><%@page import="java.util.List" %><%@page import="java.net.URLDecoder" %><%@page import="be.destin.util.Util" %><%
   SkosHtmlManager skosHtmlManager = SkosHtmlManager.getAskosiSessionInstance(request);
   SkosManager skosManager = skosHtmlManager.getSkosManager();
   String applic = request.getParameter("application");
   String user = request.getRemoteUser();
   String comment = request.getParameter("comment");
   String remove = request.getParameter("remove");
   boolean toRemove = (comment==null)||comment.isEmpty();
   if (remove != null && !remove.isEmpty()) {
	   remove = remove.toLowerCase();
	   if (remove.startsWith("on") || remove.startsWith("true") ) {
		   toRemove = true;
	   }
   }
   String rec = request.getParameter("record");
   String url = request.getParameter("url");
   UsageLogger usageLogger = new UsageLogger(request,applic,rec,UsageMode.COMMENT);
   if (url != null && !url.isEmpty()) {
		UsageManager.fillUsageLogger(skosManager, usageLogger, URLDecoder.decode(url));
		applic = usageLogger.getApplication();
		rec = usageLogger.getRecord();
   }
   usageLogger.setInfoMessage(comment);
   %>{"user":"<%=usageLogger.getUser() %>", "application":"<%=applic%>","record":"<%=rec%>","comment":"<%=Util.escapeJSON(comment)%>", <%
   Log[] currentTags = usageLogger.getLoggerRec();
   Log currentTag = null;
   if (currentTags != null && currentTags.length > 0) currentTag = currentTags[0];
   if (toRemove && currentTag != null) {
	   usageLogger.getContext().deleteObject(currentTag);
	   %>"action":"removed", "status":"notset", <%
   } else if (!toRemove && currentTag == null) {
	   if (usageLogger.getContext() != null) usageLogger.create();
	   %>"action":"added","status":"set", <%
   } else {
	   if (currentTag != null) {
		   usageLogger.update(currentTag);
		   %>"action":"updated", "status":"set", <%
	   } else {
		   %>"action":"unchanged", "status":"notset", <%
	   }
   }
   %>"time":"<%=usageLogger.getNow() %>"<%
%>}